namespace PasswordGenerator
{
    using System;
    using System.Windows.Forms;
    using PasswordGenerator.Generator;

    public partial class PasswordGeneratorForm : Form
    {
        private PasswordGenerator generator;

        /// <summary>
        /// Initializes a new instance of the <see cref="PasswordGeneratorForm"/> class.
        /// </summary>
        public PasswordGeneratorForm()
        {
            this.InitializeComponent();
        }

        private void PasswordGeneratorForm_Load(object sender, System.EventArgs e)
        {
            generator = new PasswordGenerator(new RngCryptoProvider());

            generator.UseSmallCharacters = smallCharactersCheck.Checked;
            generator.UseCapitalCharacters = bigCharactersCheck.Checked;
            generator.UseNumbers = numbersCheck.Checked;
            generator.UseSpecialCharacters = specialCharactersCheck.Checked;
            generator.ApplyKeyboardLayoutCheck(keyboardLayoutCheck.Checked, "en-EN", "de-DE");
            generator.PasswordLength = (uint)passwordLength.Value;
        }

        private void passwordLength_ValueChanged(object sender, System.EventArgs e)
        {
            generator.PasswordLength = (uint)passwordLength.Value;
        }


        private void smallCharactersCheck_CheckedChanged(object sender, System.EventArgs e)
        {
            generator.UseSmallCharacters = smallCharactersCheck.Checked;
        }

        private void bigCharactersCheck_CheckedChanged(object sender, System.EventArgs e)
        {
            generator.UseCapitalCharacters = bigCharactersCheck.Checked;
        }

        private void numbersCheck_CheckedChanged(object sender, System.EventArgs e)
        {
            generator.UseNumbers = numbersCheck.Checked;
        }

        private void specialCharactersCheck_CheckedChanged(object sender, System.EventArgs e)
        {
            generator.UseSpecialCharacters = specialCharactersCheck.Checked;
        }

        private void generatePasswordCommand_Execute(object sender, EventArgs e)
        {
            generator.Generate();
            password.Text = generator.Password;
        }

        private void generateAndCopyCommand_Execute(object sender, EventArgs e)
        {
            generator.Generate();
            password.Text = generator.Password;
            password.SelectionStart = 0;
            password.SelectionLength = password.Text.Length;

            password.Copy();

            password.SelectionStart = 0;
            password.SelectionLength = 0;
        }

        private void keyboardLayoutCheck_CheckedChanged(object sender, EventArgs e)
        {
            generator.ApplyKeyboardLayoutCheck(keyboardLayoutCheck.Checked, "en-EN", "de-DE");
        }
    }
}
